import { Proc } from "./Proc";
import { ProcInit } from "./ProcInit"
import { WXSDK } from "../Framework/SDK/Wx/WxLogin";
import { SDK } from "../Framework/SDK/ISDK";
import { DummySDK } from "../Framework/SDK/DummySDK/DummySDK";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Mainloop extends cc.Component {

    static instance: Mainloop;

    curProc: Proc = null;
    
    // LIFE-CYCLE CALLBACKS:

    onLoad () {         
        cc.game.addPersistRootNode(this.node);
        Mainloop.instance = this;
        if(cc.sys.platform === cc.sys.WECHAT_GAME)
        {
            SDK.instance = new WXSDK();
        }
        else
        {
            SDK.instance = new DummySDK();
        }

        this.changeProc(new ProcInit());
    }

    start () {

    }

    async changeProc(newProc: Proc)
    {
        if(this.curProc != null)
        {
            this.curProc.onLeave();
        }
        this.curProc = null;
        let ret: boolean = await newProc.onEnter();
        if(ret)
            this.curProc = newProc;
    }

    update (dt) {
        if(this.curProc != null)
        {
            this.curProc.onProcess(dt);
        }
    }
}
